package com.sdy.hospitalguard.embed.keepalive.manager.netty.factory;

import com.alibaba.fastjson.JSONObject;
import com.sdy.hospitalguard.embed.keepalive.manager.netty.factory.base.BaseChannelFactory;
import com.sdy.hospitalguard.embed.keepalive.pojo.netty.FastGateChannelHandlerContext;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.Objects;

@Slf4j
public class ChannelFactory extends BaseChannelFactory {

    static {
        log.debug("================ ChannelFactory ================");
    }

    /**
     * 删除通道
     *
     * @param serialNo 设备编码
     */
    public static boolean removeChannelByDeviceCode(String serialNo) throws InterruptedException {
        FastGateChannelHandlerContext fctx = CHANNEL_MAP.get(serialNo);
        if (Objects.nonNull(fctx))
        {
            fctx.setCtx(null);
            fctx.setLock(false);
            return true;
        }else{
            //CPU
            Thread.sleep(1);
        }
        log.error("[removeChannelByDeviceCode][DEVICE_CODE = {}]， tcp keepalive connect is don't exit, ", serialNo);
        return false;
    }

    /**
     * 新增在线通道
     * @param serialNo
     * @param ctx
     */
    public static void addChannel(String serialNo, ChannelHandlerContext ctx){
        if (StringUtils.isNotBlank(serialNo) && Objects.nonNull(ctx)){
            FastGateChannelHandlerContext fctx = CHANNEL_MAP.get(serialNo);
            if (Objects.nonNull(fctx)){
                fctx.setCtx(ctx);
                fctx.setLock(false);
            }else{
                fctx = new FastGateChannelHandlerContext(ctx, 0);
                CHANNEL_MAP.put(serialNo, fctx);
            }
        }else{
            log.error("[addChannel]，add active channel is error , because serialNo = {} or channel handler context = {}", serialNo, ctx);
        }
    }

    /**
     *  获取  速通门 长连接 通道对象
     *
     * @param serialNo
     * @return
     */
    public static FastGateChannelHandlerContext getChannelBySerialNo(String serialNo){
        if (StringUtils.isBlank(serialNo)){
            log.error("[getChannelBySerialNo]，get active channel binding error , because serialNo = {} ", serialNo);
            return null;
        }
        return CHANNEL_MAP.get(serialNo);
    }

    /**
     * 解锁设备编码为 serialNo 的设备长连接，
     *
     * @param serialNo
     * @throws InterruptedException
     */
    public static void unLockByDeviceCode(String serialNo) throws InterruptedException {
        FastGateChannelHandlerContext fctx = CHANNEL_MAP.get(serialNo);
        if (Objects.nonNull(fctx)){
            fctx.setLock(false);

        }else{
            // CPU
            Thread.sleep(1);
        }
    }

    /**
     * 刷新链接
     * @param serialNo
     * @param ctx
     */
    public static void reFrushChannel(String serialNo, ChannelHandlerContext ctx){
        FastGateChannelHandlerContext fctx = CHANNEL_MAP.get(serialNo);
        if (Objects.nonNull(fctx)){
            log.debug("[SERIAL_NO = {}], refush tcp connection is success ", serialNo);
            fctx.setCtx(ctx);
        }else{
            log.error("[SERIAL_NO = {}], get active tcp keepalive connection error，exit connection : {}", serialNo, JSONObject.toJSONString(CHANNEL_MAP));
        }
    }

}
